package cn.fj.org.sevice.impl;

import cn.fj.basic.service.impl.BaseServiceimpl;
import cn.fj.basic.util.PageList;
import cn.fj.org.domain.Department;
import cn.fj.org.mapper.DepartmentMapper;
import cn.fj.org.query.DepartmentQuery;
import cn.fj.org.sevice.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceimpl extends BaseServiceimpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentmapper;

    @Transactional
    @Override
    public void add(Department department) {
        departmentmapper.save(department);//添加数据的时候，获取主键自增
       //调用update
        update(department);

    }


    @Transactional
    @Override
    public void update(Department department) {

        if(department.getParent()==null){
            //改造
            department.setDirPath("/"+department.getId());
        }else {
            //2种，有父部门,通过id查找到上级部门对应的path，在和自己拼接上
            //查询数据
            if(department.getParent().getId()!=null){
                Department dbDepartment = departmentmapper.loadById(department.getParent().getId());
                department.setDirPath(dbDepartment.getDirPath()+"/"+dbDepartment.getId());

            }else {
                //改造
                department.setDirPath("/"+department.getId());
            }

        }
        departmentmapper.update(department);

    }


    @Override
    public List<Department> depTree() {
        List<Department> departments = departmentmapper.depTree();
        return departments;
    }
}
